function DojoUtil(){
    
}
/**
 * เช็คดูว่า gridId ที่ส่งมาอ่ะ มีอย่างน้อย 1 แถวหรือเปล่า ที่ถูกเลือกเอาไว้
 * @param gridId
 * @returns {Boolean}
 */
DojoUtil.prototype.isGridHasSelectedAtLeastOneRow = function (gridId) {
    grid = dijit.byId(gridId);   
    if (grid.selection.getSelectedCount() > 0) {
        if(grid.selection.getSelected() == false){
            alertBox(MESSAGES.MSG_DELETE_SELECT_ROW);
            return false;
        }
        return true;
    } else {
        alertBox(MESSAGES.MSG_DELETE_SELECT_ROW);
        return false;
    }
};
DojoUtil.prototype.getJsonIdsFromGridSelectedRows = function (jsonId, fieldId, gridId) {
    grid = dijit.byId(gridId);

    len = grid.selection.getSelectedCount();

    var id = this.getValuesFromGridSelectedRows(fieldId, gridId);

    var data = '';
    for ( var i = 0; i < len; i++) {
        if (i > 0) {
            data += ",";
        }
        data += '{"' + jsonId + '":"' + id[i] + '"}';
    }
    return data;
};
DojoUtil.prototype.getValuesFromGridSelectedRows = function (fieldId, gridId) {
    grid = dijit.byId(gridId);
    store = grid.store;
    rows = grid.selection.getSelected();

    len = grid.selection.getSelectedCount();

    var values = new Array();
    var i = 0;

    for (i = 0; i < len; i++) {
        values[i] = store.getValue(rows[i], fieldId);
    }

    return values;
};
DojoUtil.prototype.generalSearchItemsList = '';
DojoUtil.prototype.getJsonIdsFromGrid = function (jsonId, fieldId, gridId) {
    DojoUtil.generalSearchItemsList = '';

    // var pantryStore = new dojo.data.ItemFileReadStore({ url:
    // "pantry_items.json" });
    var pantryStore = dijit.byId(gridId).store;
    // Define a callback that fires when all the items are returned.
    var gotList = function(items, request) {
        var ii = 0;
        dojo.forEach(items, function(i) {
            if (ii > 0) {
                generalSearchItemsList += ",";
            }
            DojoUtil.generalSearchItemsList += '{"' + jsonId + '":"'
            + pantryStore.getValue(i, fieldId) + '"}';
            ii++;
        });
        console.debug("#First Chief: All items are: " + generalSearchItemsList);
    };

    var gotError = function(error, request) {
        //alert("The request to the store failed. " + error);
        console.error("The request to the store failed. " + error);
    };

    // Invoke the search
    pantryStore.fetch({
        onComplete : gotList,
        onError : gotError
    });

    return generalSearchItemsList;
};
var generalSearchItemsList;
DojoUtil.prototype.getValuesFromGrid = function (fieldId, gridId) {
    generalSearchItemsList = '';
    var values = new Array();

    var pantryStore = dijit.byId(gridId).store;

    var gotList = function(items, request) {
        var ii = 0;
        dojo.forEach(items, function(i) {
            if (ii > 0) {
                generalSearchItemsList += ",";
            }
            values[ii] = pantryStore.getValue(i, fieldId);
            ii++;
        });
        console.debug("#First Chief: All items are: " + generalSearchItemsList);
    };

    var gotError = function(error, request) {
        //alert("The request to the store failed. " + error);
        console.error("The request to the store failed. " + error);
    };

    // Invoke the search
    pantryStore.fetch({
        onComplete : gotList,
        onError : gotError
    });

    return values;
};

/**
 * get html from url
 */
DojoUtil.prototype.getHtml = function (url, handleLoad) {
    var xhrArgs = {
        url : url,
        urlPreventCache : true, 
        clearOnClose : true,
        handleAs : "text",
        headers : {
            "Content-Type" : "text/html; charset=UTF-8"
        },
        load : function(res){
            handleLoad(res);
        },
        error : function(error) {
            //alert(error);
            console.error(error);
        }
    };
    dojo.xhrGet(xhrArgs);
};

/**
 * private function
 */
this.handleMessageBox1 = function (htmlText, title, headerMessage, arrayOfRows){
    var newHtmlText = htmlText;
    newHtmlText = newHtmlText.replace('${icon}', 
        '<img src="../../' + koolDiscoveryContext + '/icons/information.png" alt="" style="vertical-align:middle;">');
    newHtmlText = newHtmlText.replace('${message}', headerMessage);
        
    var bodyMessage = '<div><table>';
    for(var i=0;i<arrayOfRows.length;i++){
        bodyMessage += '<tr><td>';
        bodyMessage += arrayOfRows[i];
        bodyMessage += '</td></tr>';
    }
    bodyMessage += '</table></div>';
    newHtmlText = newHtmlText.replace('${body}', bodyMessage);
    //alert(newHtmlText);
    return newHtmlText;
}

/**
 * message box1 : one header and many child rows
 */
DojoUtil.prototype.messageBox1 = function(title, headerMessage, arrayOfRows){
    this.messageBox1WithHandleFunc(title, headerMessage, arrayOfRows, function(){});
};
DojoUtil.prototype.messageBox1WithHandleFunc = function(title, headerMessage, arrayOfRows, handleFunc){
    this.getHtml('../../' + koolDiscoveryContext + '/resources/message_body1.html', function(htmlText){
        var newHtmlText = handleMessageBox1(htmlText, title, headerMessage, arrayOfRows);
        
        var widgetId = 'messageBox1';
        var dialog = new dijit.Dialog({
            id: widgetId,
            draggable : false,
            style : '',
            onHide : function() {
                dijit.byId(widgetId).destroyRecursive();
            },
            onLoad : function() {
				alert('c');
				dijit.byId(this.id).layout();
				alert('b');
                dojo.connect(dijit.byId('messageBox1OkBtn'), "onClick", handleFunc);
            }
        });
        dialog.set('title', title);
        dialog.set('content', newHtmlText);
        dialog.show();
    });
};

/*******************************************************************************
 * utilities function by First Chief
 ******************************************************************************/